define(function () {
    'use strict';

    function ctrl($scope, $state, CommonService, LocalCacheService) {
        function init() {
            $scope.financingLoan = {
                needMarketValue: 122000.00,
                choseMarketValue: 0
            };
            $scope.sharesList = [{
                name: '中国联通 600060',
                count: 125000,
                price: 125.00,
                marketValue: 0,
                rate: 60,
                counterFee:15.50,
                value: 0,
                allCheck: false,
                checked: false
            }, {
                name: '中国联通 600060',
                count: 125000,
                price: 125.00,
                marketValue: 0,
                rate: 60,
                counterFee:15.50,
                value: 0,
                allCheck: false,
                checked: false
            }]
        }

        $scope.$on('$ionicView.loaded', function () {
            init();
        });

        function allValue(){
            var allValue = 0;
            for (var i = 0; i < $scope.sharesList.length; i++) {
                if ($scope.sharesList[i].checked) {
                    allValue += $scope.sharesList[i].price * $scope.sharesList[i].value;
                }
            }
            $scope.financingLoan.choseMarketValue = allValue;
        }

        $scope.check = function () {
            allValue();
        };

        $scope.plus = function (index) {
            $scope.sharesList[index].value += 100;
            $scope.sharesList[index].marketValue = $scope.sharesList[index].value * $scope.sharesList[index].price;
            allValue();
        };

        $scope.minus = function (index) {
            $scope.sharesList[index].value -= 100;
            $scope.sharesList[index].marketValue = $scope.sharesList[index].value * $scope.sharesList[index].price;
            allValue();
        };

        $scope.all = function (index) {
            if ($scope.sharesList[index].allCheck) {
                $scope.sharesList[index].value = $scope.sharesList[index].count;
            } else {
                $scope.sharesList[index].value = 0;
            }
            $scope.sharesList[index].marketValue = $scope.sharesList[index].value * $scope.sharesList[index].price;
            allValue();
        };

        $scope.submit = function () {
            $state.go('tab.account-easyFinancingManual');
        }
    }

    ctrl.$inject = ['$scope', '$state', 'CommonService', 'LocalCacheService'];
    return ctrl;
});
